package cn.yqx.bankshop.goods.controller;

import cn.yqx.bankshop.goods.pojo.BuActivityCenter;
import cn.yqx.bankshop.goods.pojo.BuBanner;
import cn.yqx.bankshop.goods.pojo.BuProductType;
import cn.yqx.bankshop.goods.pojo.BuSearchRecord;
import cn.yqx.bankshop.goods.service.*;
import cn.yqx.bankshop.result.Result;
import cn.yqx.bankshop.result.ResultCode;
import com.alibaba.fastjson.JSONArray;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;
@Slf4j
@RestController
@RequestMapping("/api/v1/index")
public class IndexController {
    @Autowired
    IndexService indexService ;
    @Autowired
    BannerService bannerService ;
    @Autowired
    SearchKeyWordService searchKeyWordService ;
    @Autowired
    ActivityCenterService activityCenterService ;
    @Autowired
    GoodsService goodsService ;

    @RequestMapping(value = "/find_product" , method = RequestMethod.GET)
    public Object getProductList(int grade , @RequestParam(value = "parProductId", required = false ) Long parProductId){

        Result result ;
        try {
            result = Result.success();
            List<BuProductType> list = indexService.getProductList(grade,parProductId) ;
            result.setData(list);
        }catch (Exception e){
            result = Result.failed(ResultCode.SYSTEM_EXECUTION_ERROR) ;
            log.error("error : {} " ,e );
        }
        return  result;
    }

    @RequestMapping(value = "/find_product_son" ,method = RequestMethod.GET)
    public Object getProductSonList(Long  parProductId) {
        Result result ;
        try{
            result = Result.success();
            JSONArray jsonArray = new JSONArray() ;
            jsonArray = indexService.getProductSonList(parProductId) ;
            result.setData(jsonArray);

        }catch (Exception e){
            result = Result.failed(ResultCode.SYSTEM_EXECUTION_ERROR) ;
            log.error("error : {} " ,e );
        }

        return result ;
    }

    @RequestMapping(value = "find_banners",method = RequestMethod.GET)
    public Object getBanners(){
        Result result ;
        try{
            result = Result.success() ;
            List<BuBanner> bannerList =  bannerService.getBannerList();
            result.setData(bannerList);
        }catch (Exception e){
            result = Result.failed(ResultCode.SYSTEM_EXECUTION_ERROR) ;
            log.error("error : {} " ,e );
        }
        return result ;
    }


    @RequestMapping(value = "find_search_keyword",method = RequestMethod.GET)
    public Object findSearchKeyWord(){
        Result result ;
        try{
            result = Result.success() ;
            List<BuSearchRecord> buSearchRecords = searchKeyWordService.getSearchRecordList();
            result.setData(buSearchRecords);
        }catch (Exception e){
            result = Result.failed(ResultCode.SYSTEM_EXECUTION_ERROR) ;
            log.error("error : {} " ,e );
        }

        return result ;
    }

    @RequestMapping(value = "find_activitys" , method = RequestMethod.GET)
    public Object findActivitys(){
        Result result ;
        try{
            result = Result.success() ;
            List<BuActivityCenter> buActivityCenters =  activityCenterService.findActivitys();
            result.setData(buActivityCenters);
        }catch (Exception e){
            result = Result.failed(ResultCode.SYSTEM_EXECUTION_ERROR) ;
            log.error("error : {} " ,e );
        }

        return result ;
    }
}
